<template>
  <div class="app-container">
    <el-card class="box-card" shadow="always" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" @submit.native.prevent>
        <el-form-item :label="titles[queryParams.tagType]" prop="sourceName">
          <el-input v-model="queryParams[addKeys[queryParams.tagType]]" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="content">
      <el-card shadow="always" class="mt20 table-main" :body-style="{ padding: '20px' }">
        <!-- <div slot="header">
            <span></span>
        </div> -->
        <!-- card body -->
        <div class="content">
          <el-menu :default-active="defaultActive" :default-openeds="defaultOpeneds" class="el-menu-vertical-demo" @select="menuSelect" @open="handleOpen" @close="handleClose">
            <el-sub-menu :index="index" v-for="(item, index) in navList" :key="index">
              <template #title>
                <el-icon><setting /></el-icon>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item v-for="(child, idx) in item.children" :key="idx" :index="`${index}-${idx}`">
                <el-icon><icon-menu /></el-icon>
                <span>{{ child.title }}</span>
              </el-menu-item>
            </el-sub-menu>
          </el-menu>
          <div class="table-main">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" @click="handleAdd">添加 </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="danger" :disabled="!selectedRows.length" @click="handleBatchDelete">删除 </el-button>
              </el-col>
              <right-toolbar :v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table
              v-if="refreshTable"
              row-key="id"
              :default-expand-all="isExpandAll"
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
              v-loading="loading"
              :data="dataSource"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55" align="center" :selectable="selectHandle" />
              <el-table-column label="序号" type="index" width="80" />
              <el-table-column :label="titles[queryParams.tagType]" align="left" :prop="addKeys[queryParams.tagType]" />
              <el-table-column label="备注" align="left" prop="remark" />
              <el-table-column label="操作" align="left">
                <template #default="scope">
                  <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
                  <el-divider direction="vertical"></el-divider>
                  <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <!-- <pagination v-show="total > 0" :total="total" v-model:page.sync="queryParams.pageNum"
                v-model:limit.sync="queryParams.pageSize" @pagination="getList" /> -->
      </el-card>
    </div>
    <addForm ref="modelForm" :categoryList="categoryList" @ok="getList"></addForm>
  </div>
</template>
  
<script>
import addForm from './modules/addForm.vue';
import listMixin from '@/mixins/listMixin';
import request from '@/utils/request';
export default {
  name: 'classify',
  dicts: ['inventory_status', 'counting_method'],
  components: {
    addForm
  },
  mixins: [listMixin],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tagType: 0
      },
      titles: ['供应商标签', '客户标签', '客户行业','联系人岗位'],
      addKeys: ['tagName', 'tagName', 'industryName', 'postName'],
      defaultOpeneds: [0,1],
      defaultActive:'0-0',
      navList: [
        {
          title: '客户配置',
          name: 0,
          icon: 'el-icon-edit',
          children: [
            {
              title: '供应商标签',
              name: 0,
              key: 'tagName'
            },
            {
              title: '客户标签',
              name: 1,
              key: 'tagName'
            },
            {
              title: '客户行业',
              name: 2,
              key: 'industryName'
            },
            {
              title: '联系人岗位',
              name: 3,
              key: 'postName'
            },
          ]
        }
      ],
      urlList: [
        {
          list: '/order/supplierTag/list',
          delete: '/order/supplierTag'
        },
        {
          list: '/order/supplierTag/list',
          delete: '/order/supplierTag'
        },
        {
          list: '/order/industry/list',
          delete: '/order/industry'
        },
        {
          list: '/order/post/list',
          delete: '/order/post'
        },
      ],
      isExpandAll: true,
      refreshTable: true
    };
  },
  computed: {
    urls() {
      return this.urlList[this.queryParams.tagType];
    }
  },
  methods: {
    menuSelect(index) {
      let type = index.split('-')
      this.queryParams.tagType = +type[1];
      this.getList();
    },
    handleAdd() {
      this.$refs.modelForm.add({}, this.queryParams.tagType);
    },
    handleEdit(row) {
      this.$refs.modelForm.edit(row, this.queryParams.tagType);
    },
    /**
     * @获取列表
     */
    getList() {
      this.loading = true;
      var params = {
        url: this.urls.list,
        method: 'get',
        params: this.getQueryParams() //查询条件
      };

      request(params)
        .then((res) => {
          this.loading = false;
          this.dataSource = res.rows;
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },

    /**
     * @批量删除
     */
    handleBatchDelete() {
      if (this.selectedRows.length == 0) {
        this.$modal.msgError('请选择要删除的数据');
        return;
      }
      if (this.selectedRows.some((item) => item.children && item.children.length > 0)) {
        this.$modal.msgError('请先删除子分类');
        return;
      }
      var ids = this.selectedRows.map((item) => item.id).join(',');
      this.handleDelete(ids);
    },
    resetQuery() {
      this.queryParams = {
        classifyType: '0',
        pageNum: 1,
        pageSize: 10
      };
      this.getList();
    }
  }
};
</script>
  <style scoped lang="scss">
.content {
  display: flex;
  max-height: calc(100vh - 180px);
  gap: 20px;
}
.table-main {
  width: 100%;
  height: calc(100vh - 240px);
}
</style>